// fields类型检查
import { componentMap } from '@/components/DynamicTableForm/Form/componentMap'

export const fieldsValidator = (value) => {
  return value.every(field => {
    // 必填字段检查
    const requiredFields = ['type', 'prop', 'label']
    const missingFields = requiredFields.filter(f => !(f in field))
    if (missingFields.length > 0) {
      console.error(`字段配置缺失: ${missingFields.join(', ')}`, field)
      return false
    }

    // 组件类型检查
    if (!componentMap[field.type]) {
      console.error(`未注册的组件类型: ${field.type}`, field)
      return false
    }

    // 选择类组件选项检查
    if (['select', 'checkbox-group', 'radio-group'].includes(field.type)) {
      if (!field.options || !Array.isArray(field.options)) {
        console.error(`选择类组件必须提供options数组`, field)
        return false
      }
    }

    return true
  })
}
// 获取组件事件处理器
export const getComponentEvents = (field,formData) => {
  const events = {}
  // 通用事件处理
  switch (field.type) {
    case 'radio-group':
      events.input=(value)=>{
        field.click?field.click(field.prop,value,field,formData): ''
      }
      break
    case 'switch':
      events.change=(value)=>{
        console.log('switch',value)
        field.click? field.click(field.prop,value,field,formData):''
      }
      break
    case 'date':
      break
    case 'daterange':
      events.change = (value) => {
        // this.$emit('date-change', { prop: field.prop, value })
      }
      break

    case 'cascader':
      events.change = (value) => {
        // this.$emit('cascader-change', { prop: field.prop, value })
      }
      break
  }
  // 合并自定义事件
  if (field.events) {
    Object.assign(events, field.events)
  }
  return events
}
// 获取默认占位符
export const getDefaultPlaceholder = (type) => {
  const placeholderMap = {
    input: '请输入',
    select: '请选择!',
    'checkbox-group': '请选择',
    'radio-group': '请选择',
    date: '请选择日期',
    daterange: '请选择日期范围',
    datetime: '请选择日期时间',
    cascader: '请选择',
    upload: '请选择文件',
    'input-number': '请输入数字',
    slider: '请拖动滑块',
    color: '请选择颜色',
    rate: '请评分'
  }
  return placeholderMap[type] || '请输入内容'
}
